package ui.history;

import javax.swing.JPanel;

import blservice.document.DocumentBLService;
import tool.MyButton;
import tool.MyLabel;
import ui.commodity.StockOverflowDocumentPanel;
import ui.commodity.StockScrapDocumentPanel;
import ui.commodity.StockSendDocumentPanel;
import ui.finance.收付款单Panel;
import ui.finance.现金费用单Panel;
import ui.importandsaleman.ImportAndReturnDocumentPanel;
import ui.importandsaleman.SaleAndReturnDocumentPanel;
import vo.DocumentVo;
import vo.UserVo;
import vo.StockScrapAndOverflowDocumentVo;
import vo.StockSendingDocumentVo;
import vo.AccountInAndOutDocumentVo;
import vo.CashExpensesDocumentVo;
import vo.ImportAndReturnDocumentVo;
import vo.SaleAndReturnDocumentVo;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import o.财务单据类型;
import o.进货单据类型;
import o.销售单据类型;
import client.boot.BLServiceManager;

import java.util.List;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;

public class BusinessProgressPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	// protected DocumentBLService djcxBL;
//	protected DocumentBLService hcBL;
	protected UserVo user;
	protected tool.DateChooserJButton tfFromDate;
	protected tool.DateChooserJButton tfToDate;
	protected JTextField tfCustomer;
	protected JTextField tfCountorman;
	protected JTable table;
	protected JComboBox<String> cbDocumentType;
	protected JTextField cbWarehouse;
	protected JButton detials;
	protected JButton btnRedRushAndCopy;
	protected JButton btnRedRush;
	protected List<DocumentVo> list;

	public BusinessProgressPanel(UserVo user) {
		this.user=user;
		this.setOpaque(false);
		setLayout(null);
		setSize(646, 433);
		

		JLabel lblDocumentType = new MyLabel("\u5355\u636E\u7C7B\u578B");
		lblDocumentType.setBounds(50, 10, 70, 15);
		add(lblDocumentType);

		cbDocumentType = new JComboBox<String>();
		cbDocumentType.setModel(new DefaultComboBoxModel<String>(new String[] { "销售单",
				"销售退货单", "进货单", "进货退货单", "付款单", "收款单", "库存报损单", "库存报溢单", "库存赠送单", "现金费用单" }));

		cbDocumentType.setBounds(113, 7, 78, 21);
		cbDocumentType.addActionListener(e->filter());
		add(cbDocumentType);

		JLabel lblFromDate = new MyLabel("起始时间");
		lblFromDate.setBounds(255, 10, 65, 15);
		add(lblFromDate);

		tfFromDate = new tool.DateChooserJButton();
		tfFromDate.setBounds(323, 8, 78, 19);
		add(tfFromDate);

		JLabel lblToDate = new MyLabel("\u7ED3\u675F\u65F6\u95F4");
		lblToDate.setBounds(467, 10, 70, 15);
		add(lblToDate);

		tfToDate = new tool.DateChooserJButton();
		tfToDate.setBounds(547, 8, 78, 19);
		add(tfToDate);
	
		JLabel lblCustomer = new MyLabel("\u5BA2\u6237");
		lblCustomer.setBounds(50, 35, 70, 15);
		add(lblCustomer);

		tfCustomer = new JTextField();
		tfCustomer.setBounds(113, 35, 78, 19);
		add(tfCustomer);
		tfCustomer.setColumns(10);

		JLabel lblCountorman = new MyLabel("\u4E1A\u52A1\u5458");
		lblCountorman.setBounds(255, 35, 70, 15);
		add(lblCountorman);

		tfCountorman = new JTextField();
		tfCountorman.setBounds(323, 35, 78, 19);
		add(tfCountorman);
		tfCountorman.setColumns(10);

		JLabel lblWarehouse = new MyLabel("\u4ED3\u5E93");
		lblWarehouse.setBounds(467, 35, 70, 15);
		add(lblWarehouse);

		cbWarehouse = new JTextField();
		cbWarehouse.setBounds(547, 33, 78, 19);
		add(cbWarehouse);

		JButton btnFilter = new MyButton("Graphics/Button65-23.png");
		btnFilter.setText("筛选");	
		btnFilter.addActionListener(e->filter());
		btnFilter.setBounds(560, 62, 65, 23);
		add(btnFilter);

		table = new JTable(){		
			private static final long serialVersionUID = 8999208543419910360L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			};
		};
		
		JScrollPane jsp=new JScrollPane(table);
		jsp.setBounds(47, 95, 578, 293);
		add(jsp);
		
		btnRedRush = new MyButton("Graphics/Button93-23.png");
		btnRedRush.setText("红冲");
		btnRedRush.addActionListener(e->redRush());
		btnRedRush.setBounds(300, 398, 93, 23);
		add(btnRedRush);
		
		
		btnRedRushAndCopy = new MyButton("Graphics/Button107-23.png");
		btnRedRushAndCopy.setText("红冲并复制");
		btnRedRushAndCopy.addActionListener(e->redRushAndCopy());
		btnRedRushAndCopy.setBounds(488, 398, 107, 23);
		add(btnRedRushAndCopy);
		
		detials = new MyButton("Graphics/Button93-23.png");
		detials.setText("显示详情");
		detials.addActionListener(e->showOne());
		detials.setBounds(86, 398, 93, 23);
		add(detials);
		
		filter();

	}
	
	void filter(){
		if(tfFromDate.getDate().getTime()>tfToDate.getDate().getTime()){
			JOptionPane.showMessageDialog(this, "开始时间不能大于结束时间！");
			return;
		}
		
		DocumentBLService documentBL=null;
		DocumentVo from=null;
		DocumentVo to=null;
		try{
		switch((String)cbDocumentType.getSelectedItem()){
		case "销售单":
			from=new SaleAndReturnDocumentVo();			
			to=new SaleAndReturnDocumentVo();
			from.setType(false);
			to.setType(false);
			documentBL=BLServiceManager.load销退售单BLService(null);			
			break;
		case "销售退货单":
			from=new SaleAndReturnDocumentVo();			
			to=new SaleAndReturnDocumentVo();	
			from.setType(true);
			to.setType(true);
			documentBL=BLServiceManager.load销退售单BLService(null);
			break;
		case "进货单":
			from=new ImportAndReturnDocumentVo();
			to=new ImportAndReturnDocumentVo();
			from.setType(false);
			to.setType(false);
			documentBL=BLServiceManager.load进退货单BLService(null);
			break;
		case "进货退货单":
			from=new ImportAndReturnDocumentVo();
			to=new ImportAndReturnDocumentVo();
			from.setType(true);
			to.setType(true);
			documentBL=BLServiceManager.load进退货单BLService(null);
			break;
		case "付款单":
			from=new AccountInAndOutDocumentVo();
			to=new AccountInAndOutDocumentVo();
			from.setType(true);
			to.setType(true);
			documentBL=BLServiceManager.load收付款单BLService(null);
			break;
		case "收款单":
			from=new AccountInAndOutDocumentVo();
			to=new AccountInAndOutDocumentVo();	
			from.setType(false);
			to.setType(false);
			documentBL=BLServiceManager.load收付款单BLService(null);
			break;
		case "现金费用单":
			from=new CashExpensesDocumentVo();
			to=new CashExpensesDocumentVo();
			documentBL=BLServiceManager.load现金费用单BLService(null);
			break;
		case "库存报溢单":
			from=new StockScrapAndOverflowDocumentVo();
			to=new StockScrapAndOverflowDocumentVo();
			from.setType(false);
			to.setType(false);
			documentBL=BLServiceManager.load库存报溢报损单BLService(null);
			break;
		case "库存报损单":
			from=new StockScrapAndOverflowDocumentVo();
			to=new StockScrapAndOverflowDocumentVo();
			from.setType(true);
			to.setType(true);
			documentBL=BLServiceManager.load库存报溢报损单BLService(null);
			break;
		case "库存赠送单":
			from=new StockSendingDocumentVo();
			to=new StockSendingDocumentVo();
			documentBL=BLServiceManager.load库存赠送单BLService(null);
			break;
		default:
			JOptionPane.showMessageDialog(this,"请填写正确的单据类型");
			return;
		}
		from.setDate(tfFromDate.getDate());
		from.set客户(tfCustomer.getText());
		from.set业务员(tfCountorman.getText());
		from.set仓库((String)cbWarehouse.getText());
		to.setDate(tfToDate.getDate());
		to.set客户(tfCustomer.getText());
		to.set业务员(tfCountorman.getText());
		to.set仓库((String)cbWarehouse.getText());
		list=documentBL.get(from, to);
		}catch(RemoteException | MalformedURLException | SQLException
				| NotBoundException e) {
			JOptionPane.showMessageDialog(this,"无法连接到服务器"+e.getMessage());
			e.printStackTrace();
		}		
		display();
	}
	
	void display(){
		DefaultTableModel model = new DefaultTableModel();
		model.setColumnIdentifiers(new String[] {"单据编号", "业务员","客户","仓库"});
		
		for (int i=0;i<list.size();i++) {
			model.addRow(new String[] {list.get(i).get单据编号(),list.get(i).get业务员(),list.get(i).get客户(),list.get(i).get仓库()});
			
		}
		table.setModel(model);
	}
	void showOne(){
		if(table.getSelectedRow()==-1){
			JOptionPane.showMessageDialog(this, "请选中你想查看的单据\n" );
		}else{
			showDocumentDialog(false, false,false);
		}	
		
	}
	void redRush(){
		if(table.getSelectedRow()==-1){
			JOptionPane.showMessageDialog(this, "请选中你想红冲的单据\n" );
		}else{
			showDocumentDialog(false, false,true);
		}
		
	}
	void redRushAndCopy(){
		if(table.getSelectedRow()==-1){
			JOptionPane.showMessageDialog(this, "请选中你想红冲并复制的单据\n" );
		}else{
			showDocumentDialog(true, false,true);
		}
		
	}
	
	protected void showDocumentDialog(boolean editable,boolean 审批,boolean 红冲){
		JDialog dialog=new JDialog(){
			private static final long serialVersionUID = 6480974605735904218L;

			@Override
			public void paint(Graphics g) {
				this.setBackground(new Color(0, 0, 0, 0));
				Image background = Toolkit.getDefaultToolkit().getImage(
						"Graphics/framebackground.jpg");
				g.drawImage(background, 0, 0, null);
				super.paint(g);
			}
		};
		dialog.getContentPane().setLayout(null);
		dialog.setBounds(200,200,750,480);
		JPanel panel=null;
		try{
		switch((String)cbDocumentType.getSelectedItem()){
		case "销售单":
			panel=new SaleAndReturnDocumentPanel(user, (SaleAndReturnDocumentVo)list.get(table.getSelectedRow()),editable,销售单据类型.销售单,审批,红冲);
			break;
		case "销售退货单":
			panel=new SaleAndReturnDocumentPanel(user, (SaleAndReturnDocumentVo)list.get(table.getSelectedRow()),editable,销售单据类型.销售退货单,审批,红冲);
			break;
		case "进货单":
			panel=new ImportAndReturnDocumentPanel(user, (ImportAndReturnDocumentVo)list.get(table.getSelectedRow()),editable,进货单据类型.进货单,审批,红冲);
			break;
		case "进货退货单":
			panel=new ImportAndReturnDocumentPanel(user, (ImportAndReturnDocumentVo)list.get(table.getSelectedRow()),editable,进货单据类型.进货退货单,审批,红冲);
			break;
		case "付款单":
			panel=new 收付款单Panel(user, (AccountInAndOutDocumentVo)list.get(table.getSelectedRow()),editable,财务单据类型.付款单,审批,红冲);
			break;
		case "收款单":
			panel=new 收付款单Panel(user, (AccountInAndOutDocumentVo)list.get(table.getSelectedRow()),editable,财务单据类型.收款单,审批,红冲);
			break;
		case "现金费用单":
			panel=new 现金费用单Panel(user, (CashExpensesDocumentVo)list.get(table.getSelectedRow()),editable,审批,红冲);
			break;
		case "库存报溢单":
			panel=new StockOverflowDocumentPanel(user, (StockScrapAndOverflowDocumentVo)list.get(table.getSelectedRow()),editable,审批,红冲);
			break;
		case "库存报损单":
			panel=new StockScrapDocumentPanel(user, (StockScrapAndOverflowDocumentVo)list.get(table.getSelectedRow()),editable,审批,红冲);
			break;
		case "库存赠送单":
			panel=new StockSendDocumentPanel(user, (StockSendingDocumentVo)list.get(table.getSelectedRow()),editable,审批,红冲);
			break;
		default:
			return;
		}
		}catch(ClassCastException e){
			JOptionPane.showMessageDialog(this, "请选择正确的单据类型");
			e.printStackTrace();
		}
		panel.setBounds(0, 0, 746, 433);
		dialog.getContentPane().add(panel);
		dialog.setVisible(true);
	}
}
